package com.yowasa.xiuxian.engine.message.cmd;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取描述
 */
@CMD(name = "/查看效果", scope = CmdScopeEnum.ALL,outCmd = true)
public class DescHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public void execute(SendMessage message) {
        String param = getParam(message);
        if (StringUtils.isEmpty(param)) {
            throw new HandlerException("请在指令后追加查询名称");
        }

        List<String> collect = CommonEngine.DESC.keySet().stream()
                .filter(e -> e.contains(param))
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(collect)) {
            throw new HandlerException(MessageFormat.format("未查询到[{0}]相关的效果", param));
        }
        List<String> descList = new ArrayList<>();
        collect.forEach(e -> descList.addAll(CommonEngine.DESC.get(e)));
        messageHelper.replyMys(message, descList.stream().collect(Collectors.joining("\n")));
    }

    public String desc(){
        return new StringBuffer().append("全村最好用的指令！！！\n")
                .append("可以用来查看所有物品的效果，包括道具 地图 心法 战技 怪物等等\n")
                .append("当你不知道某件东西有什么用的时候，试着查看一下吧\n")
                .toString();

    }
}
